using System;
using System.Net;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using MediaPortal.Player;
using MediaPortal.GUI.Library;
using System.Collections.Generic;
using System.ComponentModel;


namespace HDTrailerPlugin
{
    /// <summary>
    /// Summary description for YahooTrailers.
    /// </summary>
    public class AppleTrailers
    {
        public static List<ITMSClient.TrailerInfo> trailers = new List<ITMSClient.TrailerInfo>();
        public static ITMSClient.TrailerIndex currentPage;

       
        public static bool _workerCompleted = false;
        

        public void HandleHttpError(object sender, ITMSClient.HTTPErrorEventArgs e){
        	
        }
        public static void GetAppleTrailersRSS(object sender, DoWorkEventArgs e)
        {
            currentPage = null;

            _workerCompleted = false;
            string xmlurl = e.Argument.ToString();
            trailers.Clear();
            Uri pageUri = new Uri(xmlurl);
            ITMSClient.ITMSContent xmlContent = ITMSClient.ITMSHelper.GetITMSPage(pageUri);


            if (xmlContent.IsValid)
            {
            	
                currentPage = new ITMSClient.TrailerIndex("test", xmlContent);
                
                foreach (ITMSClient.TrailerInfo trailerInfo in currentPage.TrailerInfoCollection)
                {
                
                    trailers.Add(trailerInfo);

                }
                 
            }
            _workerCompleted = true;

        }



        #region ProcessTrailer
        /// <summary>
        /// Processes the trailer.
        /// </summary>
        /// <param name="obj">Obj.</param>
        /// <returns></returns>
        public static ITMSClient.TrailerInfo ProcessTrailer(object obj ,ITMSClient.TrailerSize size)
        {
            
            ITMSClient.TrailerInfo trailerInfo = (ITMSClient.TrailerInfo)obj;
            Log.Info("Processing Trailer {0}",trailerInfo.Title);
            // Lookup the trailer in the media library
            string key = trailerInfo.Title.GetHashCode().ToString();
            // Populate the rest of the meta data
            trailerInfo = ITMSClient.ITMSHelper.PopulateTrailerDetails(trailerInfo, size);

            

            return trailerInfo;
        }
        #endregion
        public static ITMSClient.TrailerInfo GetTrailerUrls(object obj)
        {
            
            ITMSClient.TrailerInfo trailerInfo = (ITMSClient.TrailerInfo)obj;            
            // Populate the rest of the meta data
            trailerInfo = ITMSClient.ITMSHelper.PopulateTrailerUrls(trailerInfo);
            return trailerInfo;
        }
    }
}
